package com.cet.spring_batch_study.jobs.fileRead;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.MultiResourceItemReader;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.batch.item.file.builder.MultiResourceItemReaderBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.PlatformTransactionManager;

import java.io.IOException;

/**
 * @author NSOC
 */

@Configuration
public class FileReadConfig {

    @Bean
    public Job playInfoIngestingJob(JobRepository jobRepository,
                                    Step ingestingPlayerInfo) {
        return  new JobBuilder("playInfoIngestingJob", jobRepository)
                .start(ingestingPlayerInfo)
                .build();
    }

    @Bean
    public Step ingestingPlayerInfo(JobRepository jobRepository,
                                    PlatformTransactionManager transactionManager,
                                    ItemReader<Player> playerFlatFileItemReader,
                                    ItemWriter<Player> displayPlayerWriter) {

        return new StepBuilder("playerInfoIngesting", jobRepository)
                .<Player, Player>chunk(3, transactionManager)
                .reader(playerFlatFileItemReader)
                .writer(displayPlayerWriter)
                .build();
    }

    @Bean
    public MultiResourceItemReader<Player> multiResourceReader(FlatFileItemReader<Player> playerFlatFileItemReaderDuplicate) throws IOException {
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resolver.getResources("classpath:temp/inputs/player-info-*.csv");
        return new MultiResourceItemReaderBuilder<Player>()
                .name("multiResourceReader")
                .delegate(playerFlatFileItemReaderDuplicate)
                .resources(resources)
                .build();
    }

    @Bean
    public FlatFileItemReader<Player> playerFlatFileItemReader() {
        return new FlatFileItemReaderBuilder<Player>()
                .name("playerFlatFileItemReader")
                .resource(new ClassPathResource("temp/inputs/player-info.csv"))
                .delimited()
                .names("id", "lastName", "firstName", "position", "birthYear", "debutYear")
                .targetType(Player.class)
                .build();
    }

    @Bean
    public FlatFileItemReader<Player> playerFlatFileItemReaderDuplicate() {
        return new FlatFileItemReaderBuilder<Player>()
                .name("playerFlatFileItemReaderDuplicate")
                .delimited()
                .names("id", "lastName", "firstName", "position", "birthYear", "debutYear")
                .targetType(Player.class)
                .build();
    }

}
